<?php
if (!Yii::app()->request->isAjaxRequest) {
    $this->pageTitle = '在线支付';
    if ($orderinfo['userID'] == Yii::app()->user->id):
        ?>
        <div class="wp tm10 cl">
            <div class="order_steps cl">
                <div class="z txt f_14">下单流程</div>
                <ul class="z order_step_list cl step2">
                    <li>1.确认订单信息</li>
                    <li class="on">2.付款</li>
                    <li>3.确认收货</li>
                    <li>4.评价并获得积分</li>
                </ul>
            </div>
            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'pay-form',
//            'target'=>'_blank',
                'action' => Yii::app()->baseUrl . '/order/pay',
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'name' => 'pay-form'
                )
            ));
            ?>
            <?php echo $form->hiddenField($model, 'orderid', array('value' => $_GET['auth'])); ?>
            <?php echo $form->hiddenField($model, 'WIDout_trade_no', array('value' => $orderinfo['id'])); ?>
            <?php echo $form->hiddenField($model, 'WIDtotal_fee', array('value' => $orderinfo['totalPrice'])); ?>
            <div class="payinfoview cl rel tm20">
                <div class="z payinfo-hideout" style="height:48px;overflow:hidden;">
                    <dl class="cl payinfo-hide">
                        <!--<dd class="l">商家名称：</dd><dd class="r">老街烧烤</dd>-->
                        <dd class="l">商品名称：</dd><dd class="r">
                            <?php
                            $keynum = count($orderinfo['orderItems']);
                            foreach ($orderinfo['orderItems'] as $key => $value) {
                                echo $value['productName'] . '×' . $value['amount'];
                                if ($keynum - 1 != $key) {
                                    echo '<br/>';
                                }
                            }
                            ?>
                        </dd>
                        <dd class="l">交易金额：</dd><dd class="r"><span class="total-price2"><?php echo number_format($orderinfo['totalPrice'], 2); ?></span>元</dd>
                        <dd class="l">购买时间：</dd><dd class="r"><?php echo $orderinfo['createDate']; ?></dd>
                        <dd class="l">收货地址：</dd><dd class="r"><?php echo $orderinfo['contachInfo']['address'] . '&nbsp;&nbsp;&nbsp;&nbsp;' . $orderinfo['contachInfo']['recipients']; ?>（收）<br/><?php echo $orderinfo['contachInfo']['mobile']; ?></dd>
                        <dd class="l">交易号：</dd><dd class="r"><?php echo $orderinfo['id']; ?></dd>
                        <dd class="l">备注：</dd><dd class="r"><?php echo $orderinfo['note']; ?></dd>
                    </dl>
                </div>
                <div class="y c_orange f_20" style="margin:10px 50px 0px 0px;">￥<span class="total-price"><?php echo number_format($orderinfo['totalPrice'], 2); ?></span></div>
                <a href="javascript:void(0);" class="abs orderinfobtn orderinfo-openbtn">订单详情</a>
            </div>
            <div class="boxhd tm10"><b>选择支付方式</b></div>
            <div class="paytypechoise tm20 cl">
                <div class="l_paytype z">
                    <ul class="cl">
                        <?php
                        echo $form->radioButtonList($model, 'paytype', array('alipay' => '<span class="icon-boxalipay"></span>'), array('template' => '<li>{beginLabel}{input}{labelTitle}{endLabel}</li>', 'separator' => '', 'container' => '', 'uncheckValue' => null));
                        ?>
                    </ul>
                    <ul class="cl">
                        <?php
//                        财付通银行编号
//                        编码	银行名称
//                        0	财付通
//                        1001	招商银行
//                        1002	中国工商银行
//                        1003	中国建设银行
//                        1004	上海浦东发展银行
//                        1005	中国农业银行
//                        1006	中国民生银行
//                        1008	深圳发展银行
//                        1009	兴业银行
//                        1010	平安银行
//                        1020	交通银行
//                        1021	中信银行
//                        1022	中国光大银行
//                        1027	广东发展银行
//                        1028	中国邮政储蓄银行（仅支持广东地区）
//                        1032	北京银行
//                        1033	网汇通
//                        1052	中国银行
//                        8001	财付通余额支付


                        $paytypearr = array(
                            '1027' => '<span class="icon-box1"></span>',
                            '1009' => '<span class="icon-box2"></span>',
                            '1008' => '<span class="icon-box3"></span>',
                            '1006' => '<span class="icon-box4"></span>',
                            '1004' => '<span class="icon-box5"></span>',
                            '1005' => '<span class="icon-box6"></span>',
                            '1003' => '<span class="icon-box7"></span>',
                            '1002' => '<span class="icon-box8"></span>',
                            '1001' => '<span class="icon-box9"></span>',
                            '1032' => '<span class="icon-box10"></span>',
                            '1021' => '<span class="icon-box11"></span>',
                            '1022' => '<span class="icon-box12"></span>',
                        );
                        echo $form->radioButtonList($model, 'paytype', $paytypearr, array('template' => '<li>{beginLabel}{input}{labelTitle}{endLabel}</li>', 'separator' => '', 'container' => '', 'uncheckValue' => null));
                        ?>
                    </ul>
                </div>
                <div class="z r_paytype">
                    <h2 class="c_orange">红包减现</h2>
                    <?php if (count($row_hb) > 0): ?>
                        <ul id="couponslist" class="cl">
                            <?php foreach ($row_hb as $key => $value) { ?>
                                <?php
                                if ($value['status'] == 0) {
                                    if ($orderinfo['totalPrice'] < $value['exemptionAmount']) {
                                        $disabled = 'disabled';
                                        $title = '订单金额过小';
                                    } else {
                                        $title = $value['exemptionAmount'] . '元红包';
                                    }
                                    print_r($disbled);
                                    echo $form->checkBoxList($model, 'coupons[]', array($value['couponsId'] . '|' . $value['exemptionAmount'] => $value['exemptionAmount'] . '元红包'), array('template' => '<li>{beginLabel}{input}{labelTitle}{endLabel}</li>', 'separator' => '', 'container' => '', 'uncheckValue' => null, 'disabled' => $disabled, 'title' => $title));
                                }
                                ?>
                            <?php } ?>
                        </ul>
                    <?php else: ?>
                        <div class="f_20">你还没有任何红包</div>
                    <?php endif; ?>
                    <div class="cl tm20">
                        <div class="z wxpaytxt" style="margin-left: 0px;">勾选要使用的红包，金额立减哦！<br/><a href="<?php echo $this->createUrl('activity/ten'); ?>">没有红包？现在领取</a></div>
                    </div>
                </div>
                <!--            <div class="z r_paytype">
                                <h2 class="c_orange">微信扫码支付</h2>
                                <div class="cl">
                                    <div class="z" align="center"><img src="<?php echo Yii::app()->baseUrl; ?>/images/canvas.png"/><br/>[仅限本人使用]</div>
                                    <div class="z wxpaytxt">使用微信“扫一扫”即可付款<br/>随身携带更安全<br/><a href="#">如何使用？</a></div>
                                </div>
                            </div>-->
                <div class="cl">
                    <?php // echo CHtml::submitButton('下一步', array('class' => 'submit submit_orange', 'name' => 'topay'));    ?>
                    <?php echo CHTML::ajaxSubmitButton('下一步', '', array('dataType' => 'json', 'async' => 'false', 'success' => 'js:function(json){if (json.callback) {eval(json.callback);}if (json.content) { $("#layer_body").html(json.content);}}'), array('class' => 'submit submit_orange', 'name' => 'topay', 'id' => 'topay')) ?>
                </div>
            </div>
            <?php $this->endWidget(); ?>
            <div class="cl tm20 pay_question">
                <h3>付款遇到问题：</h3>
                <dl>
                    <dt>1.没有支付宝怎么办？</dt>
                    <dd>答：可以到<a href="#">支付宝官网</a>申请开通，支付宝可以快捷的绑定你的银行卡，以便完成交易。</dd>
                    <dt>2.通过网上银行支付，需要什么条件？</dt>
                    <dd>答：您可使用储蓄卡快捷支付轻松完成付款。目前暂时支持工行、农行、交行，其他银行储蓄卡快捷支付即将推出。</dd>
                    <dt>3.微信支付怎么操作？</dt>
                    <dd>
                        答：微信支付只需要通过微信的“扫一扫”功能，扫描页面上的二维码，按照提示操作即可完成支付。
                        <a target="_blank" href="https://katong.alipay.com/agreement/chooseBank.htm">马上开通微信</a>
                    </dd>
                    <dt>4.如何在微信中绑定银行卡？</dt>
                    <dd>答：<a href="#">点击这里</a>查看详细的绑定流程。</dd>
                </dl>
                <div class="help_more">
                    <a seed="link-cashierHelp-moreHelp" target="_blank" href="http://help.lab.alipay.com/lab/index.htm">更多帮助</a>
                </div>
            </div>
        </div>
    <?php else: ?>
        <div class="wp tm10 cl">
            <div class="nfl">
                <div class="f_c altw">
                    <div id="messagetext" class="alert_error">
                        <p>订单信息错误</p>
                        <p class="alert_btnleft"><a href="<?php echo Yii::app()->baseUrl; ?>">返回首页</a></p>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>
    <script>
        $(document).ready(function() {
            var totprice = $(".total-price").text();
            $("input[type='submit'][name='topay']").attr("id", "topayquick");
            $("#couponslist").find("input[type='checkbox']").click(function() {
                var hbprice = 0;
                var m;
                var chks = [];
                chks = $("#couponslist").find("input:checked");

                //            console.log(chks);
                //            alert(totprice);
                for (m = 0; m < chks.length; m++) {
                    var vals = chks.eq(m).val();
                    var valarr = vals.split('|');
                    hbprice += parseInt(valarr['1']);
                    //                console.log(valarr);
                }
                if (hbprice > 0) {
                    $("input[type='submit'][name='topay']").attr("id", "topay");
                } else {
                    $("input[type='submit'][name='topay']").attr("id", "topayquick");
                }
                var newprice = Number(totprice) - Number(hbprice);
                $(".total-price").text(newprice.toFixed(2));
                $(".total-price2").text(newprice.toFixed(2));
                $("#PayForm_WIDtotal_fee").val(newprice.toFixed(2));
            });
        });
    </script>
    <?php
}else {
    if (count($value['coupons']) > 0) {
        $html['callback'] .= 'ajaxWindowQuick("提示信息");';
        $html['content'] .= '<div style="text-align:center;">本次支付使用了红包，是否确定使用？</div>';
        $html['content'] .= '<form id="pay-form" method="post" action="' . Yii::app()->baseUrl . '/order/pay">';
        $html['content'] .= '<input id="PayForm_orderid" type="hidden" name="PayForm[orderid]" value="' . $value['orderid'] . '">';
        $html['content'] .= '<input id="ytPayForm_paytype" type="hidden" name="PayForm[paytype]" value="' . $value['paytype'] . '">';
        $html['content'] .= '<input id="PayForm_WIDout_trade_no" type="hidden" name="PayForm[WIDout_trade_no]" value="' . $value['WIDout_trade_no'] . '">';
        $html['content'] .= '<input id="PayForm_WIDtotal_fee" type="hidden" name="PayForm[WIDtotal_fee]" value="' . $value['WIDtotal_fee'] . '">';
        foreach ($value['coupons'] as $key => $val) {
            $html['content'] .= '<input id="PayForm_coupons_' . $key . '" type="hidden" name="PayForm[coupons][]" value="' . $val . '">';
        }
        $html['content'] .= '<div class="tm15" style="text-align:center;">';
        $html['content'] .= '<input id="topay2" class="submit submit_orange" type="submit" value="确定支付" name="topay2">';
        $html['content'] .= '</div>';
        $html['content'] .='</form>';
    } else {
        $html['callback'] .= 'document.forms["pay-form"].submit();';
    }
    echo CJSON::encode($html);
}
?>
